package tests.storage;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import storage.FileHandler;

import data.Event;
import data.EventList;
import data.EventDateTime;

public class FileHandlerTests {
	//TODO keep adding test cases
	@Before
	public void setUp() throws Exception {
		
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testWriteObject() {
		EventList newList = new EventList();
		assertTrue(FileHandler.writeObject(newList, "harsha"));
	}

	@Test
	public void testReadObject() {
		EventList newList = new EventList();
		FileHandler.writeObject(newList, "harsha");
		
		ArrayList<Event> newListEvents = newList.getEvents();
		
		EventList readList = (EventList) FileHandler.readObject("harsha");
		
		ArrayList<Event> readListEvents = readList.getEvents();
		//if(readList.getName().equals(newList.getName()))
				//assert(true);
		assertTrue(readListEvents.equals(newListEvents));	
	}

	@Test
	public void testWrite1() {
		
		EventList newList = new EventList("harsha");
		FileHandler.writeObject(newList, newList.getName());
		
		ArrayList<Event> newListEvents = newList.getEvents();
		
		EventList readList = (EventList) FileHandler.readObject("harsha");
		ArrayList<Event> readListEvents = readList.getEvents();
		assertEquals(newList.getName(), readList.getName());
		assertEquals(newListEvents, readListEvents);
	}
	
	@Test
	public void testRemoveOldText() throws IOException {
		FileHandler.removeOldText();
		
		File newFile = new File("EventsInDatabase.txt");
		assertTrue(newFile.createNewFile());
	}
	@Test
	public void testWriteToText1() throws IOException {
		FileHandler.removeOldText();
		String testString = "I am going to the party at 5pm with Someone";
		FileHandler.writeToText(testString);
		
		BufferedReader read = new BufferedReader(new FileReader("EventsInDatabase.txt"));
		String newString = read.readLine();
		assertTrue(!newString.equals(testString));
	}
	
	@Test
	public void testCreateSha1Checksum() {
		FileHandler.writeToText(FileHandler.getSha1Checksum("QuickToDo.zip"), "build.checksum");
	}
	
	@Test
	public void testUnzipFile() {
		FileHandler.unzipFile("QuickToDo2.zip");
	}
}

